package com.qiguliuxing.dts.merchant.controller;

import com.qiguliuxing.dts.merchant.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.merchant.support.AuthSupport;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.db.domain.DtsStorage;
import com.qiguliuxing.dts.service.base.DtsStorageService;
import com.qiguliuxing.dts.service.third.StorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/merchant/storage")
@Validated
@Api(tags= ApiAdminConsts.Storage.PARENT)
public class MerchantStorageController {
	private static final Logger logger = LoggerFactory.getLogger(MerchantStorageController.class);

	@Autowired
	private StorageService storageService;
	@Autowired
	private DtsStorageService dtsStorageService;

	@PostMapping("/create")
	@RequiresPermissions("merchant:storage:create")
	@RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "上传")
	@ApiOperation(value = ApiAdminConsts.Storage.CREATE)
	@WebLog(description = ApiAdminConsts.Storage.CREATE, persistence = true)
	public Object create(@RequestParam("file") MultipartFile file) throws IOException {
		logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 系统管理->对象存储->上传,请求参数,file:{}", file.getOriginalFilename());
		String originalFilename = file.getOriginalFilename();
		String url = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(),
				originalFilename);
		Map<String, Object> data = new HashMap<>();
		data.put("url", url);
		return ResponseUtils.ok(data);
	}

	@PostMapping("/merchantPicUpload")
	@ApiOperation(value = "商户入驻图片上传")
	public Object merchantPicUpload(@RequestParam("file") MultipartFile file) throws IOException {
		String originalFilename = file.getOriginalFilename();
		String url = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(),
				originalFilename);
		Map<String, Object> data = new HashMap<>();
		data.put("url", url);
		return ResponseUtils.ok(data);
	}

	@PostMapping("/read")
	@RequiresPermissions("merchant:storage:read")
	@RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "详情")
	@ApiOperation(value = ApiAdminConsts.Storage.READ)
	@WebLog(description = ApiAdminConsts.Storage.READ)
	public Object read(@ApiParam(value = "存储对象id") @NotNull Integer id) {
		DtsStorage storageInfo = dtsStorageService.findById(id);
		if (storageInfo == null) {
			return ResponseUtils.badArgumentValue();
		}
		return ResponseUtils.ok(storageInfo);
	}

}
